package com.saifan.wyy_ov.ui.zoomimage;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.ui.view.TouchImageView;
import com.saifan.wyy_ov.ui.view.TouchImageView.OnSingleTapListener;
import com.saifan.wyy_ov.utils.ToastUtil;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * @author WangLi
 * @date 2015年3月19日 下午3:02:04
 * @Description 查看大图Activity
 * @version V1.0
 */

public class ZoomImageActivity extends BaseActivity {
	private ArrayList<String> urls;
	private int position = 0;
	private TextView tip;
	private String tipText;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_viewpager_img);
		urls = getIntent().getStringArrayListExtra("img");
		position = getIntent().getIntExtra("position", 0);
		ExtendedViewPager mViewPager = (ExtendedViewPager) findViewById(R.id.view_pager);
		tip = (TextView) findViewById(R.id.viewpager_tip);
		tipText = getResources().getString(R.string.viewpager_tip);
		tip.setText(String.format(tipText, position + 1, urls.size()));
		if (urls == null || urls.size() < 0) {
			ToastUtil.showToast(ZoomImageActivity.this, "图片错误");
		} else {
			mViewPager.setAdapter(new TouchImageAdapter(this,urls));
	        mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
				
				@Override
				public void onPageSelected(int arg0) {
					tip.setText(String.format(tipText, arg0 + 1, urls.size()));
				}
				
				@Override
				public void onPageScrolled(int arg0, float arg1, int arg2) {
				}
				
				@Override
				public void onPageScrollStateChanged(int arg0) {
				}
			});
	        mViewPager.setCurrentItem(position);
		}
        
	}

	static class TouchImageAdapter extends PagerAdapter {
		private Activity activity;
		private List<String> urls;

		public TouchImageAdapter(Activity activity, List<String> urls) {
			this.activity = activity;
			this.urls = urls;
		}

		@Override
		public int getCount() {
			return urls.size();
		}

		@Override
		public View instantiateItem(ViewGroup container, int position) {
			TouchImageView img = new TouchImageView(container.getContext());
			Picasso.with(activity).load(Api.getUrlIp()  + urls.get(position)).placeholder(R.drawable.pic_none).error(R.drawable.pic_load_fail)  .into(img);
			container.addView(img, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
			img.setOnSingalTapListener(new OnSingleTapListener() {
				
				@Override
				public void onclick() {
					activity.finish();
				}
			});
			return img;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

	}
}
